﻿package cminus.ast;

import cminus.CminusException;
import java.util.LinkedList;
import java.util.Queue;

/**
 * 该类用于打印AST结构
 *     以缩进方式打印，子结点跟在父结点之下，并比父结点多一个缩进。
 */
public class ASTPrinter extends ASTScanner {
	
	int depth = 0;
	String OutPut = new String();
	@Override
	public void visit(Program node) throws CminusException {
		node.setLayer(0);
		Queue<ASTNode> q = new LinkedList<ASTNode>();
		q.add(node);
		while (!q.isEmpty()){
			ASTNode n = q.remove();
			for(int i=0;i<n.getChildrenNum();i++){
				
				 if(n.getChildIP(i)!=null){
					 		q.add(n.getChildIP(i));
							n.getChildIP(i).setLayer(n.getLayer()+1);
				 }
			}
		}
		for (int i = 0; i < depth; i++) OutPut += "\t" ;
		OutPut += "Prgoram : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}

	@Override
	public void visit(StatementList node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "StatementList : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}
	
	@Override
	public void visit(Block node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Block : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}

	@Override
	public void visit(AssignmentStatement node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Assignment : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}
	
	@Override
	public void visit(ExpressionStatement node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Expression Statement : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}

	@Override
	public void visit(IfStatement node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "If : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}

	@Override
	public void visit(WhileStatement node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "While : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}
	
	@Override
	public void visit(BreakStatement node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Break "+"   layer:" +node.getLayer()+"\n";
	}

	@Override
	public void visit(ContinueStatement node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Continue "+"   layer:" +node.getLayer()+"\n";
	}
	
	@Override
	public void visit(InputStatement node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Input : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}

	@Override
	public void visit(OutputStatement node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Output : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}
	
	@Override
	public void visit(PosExpression node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Pos : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}

	@Override
	public void visit(NegExpression node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Neg : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}
	
	@Override
	public void visit(NotExpression node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Not : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}
	
	@Override
	public void visit(AddExpression node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Add : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}
	
	@Override
	public void visit(SubExpression node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Sub : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}
	
	@Override
	public void visit(MulExpression node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Mul : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}

	@Override
	public void visit(LessExpression node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Less : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}
	
	@Override
	public void visit(LessEqualExpression node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Less Equal : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}

	@Override
	public void visit(GreaterExpression node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Greater : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}

	@Override
	public void visit(GreaterEqualExpression node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Greater Equal : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}

	@Override
	public void visit(EqualExpression node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Equal : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}

	@Override
	public void visit(NotEqualExpression node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Not Equal : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}

	@Override
	public void visit(AndExpression node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "And :" +"   layer:" +node.getLayer()+"\n";;
		depth++;
		super.visit(node);
		depth--;
	}
	
	@Override
	public void visit(OrExpression node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Or : "+"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}
	
	@Override
	public void visit(Identifier node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Identifier : " + node.getName() +"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}

	@Override
	public void visit(IntegerLiteral node) throws CminusException {
		for (int i = 0; i < depth; i++) OutPut += "\t";
		OutPut += "Literal : " + node.getValue()  +"   layer:" +node.getLayer()+"\n";
		depth++;
		super.visit(node);
		depth--;
	}
	
	public String getOutPut(){
		return OutPut;
	}
}
